package com.bridgeintelligent.tag.webserver.dim.mgmt.service.mine;

import com.bridgeintelligent.tag.mapper.*;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.utils.SecurityHelper;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by JackCheung on 2019/9/13 4:06 PM.
 */
@Service
@AllArgsConstructor
public class DimMineService implements IDimMineService {

    private DimMineMapper dimMineMapper;
    private DimMapper dimMapper;
    private CatalogMapper catalogMapper;
    private DimThemeMapper dimThemeMapper;
    private DepartmentMapper departmentMapper;

    @Override
    public DimRep list(Integer pageNumber, Integer pageSize, String dimName, String department, String theme, String createTimeStart, String createTimeEnd, String uptTimeStart, String uptTimeEnd) {
        User user = SecurityHelper.currentUser();
        String userId = user.getUserId();
        Map<String, String> catalogMap = getCatalogMap();
        Map<String, String> themeMap = getThemeMap();
        Map<String, String> departmentMap = departmentMap();
        if (StringUtils.isNotEmpty(theme)) {
            String result = getReversalThemeMap().get(theme);
            theme = result == null ? theme : result;
        }
        if (StringUtils.isNotEmpty(department)) {
            String result = departmentMapper.findOne(department);
            department = result == null ? department : result;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(calendar.MONTH, -1);
        String temp = format(calendar.getTime());
        Page page = PageHelper.startPage(pageNumber, pageSize);
        List<DimData> dimList = dimMapper.findDimsByCollect(userId, theme, department, createTimeStart, createTimeEnd, uptTimeStart, uptTimeEnd, dimName);
        dimList.forEach(dimData -> {
            dimData.setCatalogName(catalogMap.get(dimData.getCatalogId()));
            //dimData.setThemeCNName(themeMap.get(dimData.getTheme()));
            //dimData.setBusinessDepartmentCNName(departmentMap.get(dimData.getBusinessDepartment()));
            dimData.setSkillDepartmentCNName(departmentMap.get(dimData.getSkillDepartment()));
            if (temp.compareTo(dimData.getUptTime()) < 1) {
                dimData.setIsnew(true);
            } else {
                dimData.setIsnew(false);
            }
        });
        DimPage<DimData> dimDimPage = new DimPage<>();
        dimDimPage.setContent(dimList);
        dimDimPage.setTotalPages(page.getPages());
        dimDimPage.setTotalElements(page.getTotal());
        dimDimPage.setSize(page.getPageSize());
        dimDimPage.setRequestPager(pageNumber);
        return new DimRep<>(dimDimPage, BaseCode.SUCCESS);
    }

    @Override
    public DimRep add(String dimId) {
        User user = SecurityHelper.currentUser();
        String userId = user.getUserId();
//        String userId = "10003";
        int result = dimMineMapper.findExits(dimId, userId);
        if (result > 0) {
            return new DimRep(BaseCode.FAIL, "已经收藏过此标签！");
        }
        DimMine dimMine = new DimMine();
        dimMine.setDimId(dimId);
        dimMine.setId(IdWorker.nextId());
        dimMine.setUserId(userId);
        dimMineMapper.add(dimMine);
        DimData dimData = new DimData();
        dimData.setDimId(dimId);
        dimData.setIscollect(true);
        dimMapper.update(dimData);
        return new DimRep<>(dimMine, BaseCode.SUCCESS);
    }

    @Override
    public DimRep del(String dimId) {
        User user = SecurityHelper.currentUser();
        String userId = user.getUserId();
        dimMineMapper.del(dimId, userId);
        DimData dimData = new DimData();
        dimData.setDimId(dimId);
        dimData.setIscollect(false);
        dimMapper.update(dimData);
        return new DimRep(BaseCode.SUCCESS);
    }

    private Map<String, String> getCatalogMap() {
        return catalogMapper.findCatalogList().stream().collect(Collectors.toMap(DimCatalog::getId, DimCatalog::getName));
    }

    private Map<String, String> getThemeMap() {
        return dimThemeMapper.list().stream().collect(Collectors.toMap(DimTheme::getId, DimTheme::getName));
    }

    private Map<String, String> departmentMap() {
        return departmentMapper.list().stream().collect(Collectors.toMap(DimDepartment::getId, DimDepartment::getName));
    }

    private Map<String, String> getReversalThemeMap() {
        return dimThemeMapper.list().stream().collect(Collectors.toMap(DimTheme::getName, DimTheme::getId));
    }

    private static String format(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.format(date);
        } catch (Exception e) {
            return null;
        }
    }
}
